<?php

namespace App\Http\Models\Report;

use App\Http\Models\Business\InvoiceDetails;
use App\Http\Models\SalesRegion;

class InvoiceRegion extends SalesRegion
{
    public $year = 0;
    public $month = 0;

    public $real_invoice_month = 0;
    public $real_invoice_year = 0;

    public $real_invoice_last_term_year = 0;

    public $predict_invoice_year = 0;
    public $predict_invoice_month = 0;
    public $cache_invoice_regions_model = [];

    //初始化
    public function init()
    {
        $this->setRealInvoice();
        $this->set_invoice_last_term_year();
    }

    //设置获取当月实际开票数据
    private function setRealInvoice()
    {
        $real_year = 0;
        $real_month = 0;
        foreach ($this->cache_invoice_regions_model as $value) {
            if ($value->month == $this->month) {
                $real_month = $value->amount;
            }
            $real_year += $value->amount;
        }

        $this->real_invoice_month = $real_month;
        $this->real_invoice_year = $real_year;
    }


    //获取当月开票进度
    public function getProgressMonth()
    {
        $result = 0;
        if ($this->predict_invoice_month != 0) {
            $result = $this->real_invoice_month / $this->predict_invoice_month;
        }
        return $result;
    }

    //获取当年开票进度
    public function getProgressYear()
    {
        $result = 0;
        if ($this->predict_invoice_year != 0) {
            $result = $this->real_invoice_year / $this->predict_invoice_year;
        }
        return $result;
    }

    //设置获取去年开票数据
    public function set_invoice_last_term_year()
    {
        $sum = InvoiceDetails::where([
            'year' => $this->year - 1,
            ['month', '<=', $this->month],
            'sales_region_code' => $this->code
        ])->sum('total_price_no_tax');
        $this->real_invoice_last_term_year = $sum;
    }

    //获取去年同期增减
    public function get_same_term_comparison_year()
    {
        if ($this->real_invoice_last_term_year == 0) {
            return 0;
        }
        return $this->real_invoice_year / $this->real_invoice_last_term_year - 1;
    }
}
